package com.ysd.lis.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysBasDict;
import com.ysd.lis.entity.SysBasDictDetail;
import com.ysd.lis.mapper.sys.SysBasDictDetailMapper;
import com.ysd.lis.mapper.sys.SysBasDictMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.util.CodeRoleUtils;
import com.ysd.util.ToolsUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2021-10-08
 */
@Service
@Slf4j
public class SysBasDictDetailServiceImpl extends ServiceImpl<SysBasDictDetailMapper, SysBasDictDetail> implements SysBasDictDetailService {

    @Autowired
    SysBasDictDetailMapper sysBasDictDetailMapper;
    @Autowired
    SysBasDictMapper sysBasDictMapper;

    @Override
    public Result findDictDetailList(CommonPageDto pageParam) {
        MPJLambdaWrapper<SysBasDictDetail> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(SysBasDictDetail.class)
                .selectAs("b", SysBasDict::getIsEdit, SysBasDictDetail::getIsParentEdit)
                .leftJoin(SysBasDict.class, "a", SysBasDict::getId, SysBasDictDetail::getPid)
                .leftJoin(SysBasDict.class, "b", SysBasDict::getId, SysBasDict::getPid);
        if (StringUtils.isNotBlank(pageParam.getPid())) {
            queryWrapper.eq(SysBasDict::getPid, pageParam.getPid());
        } else if (StringUtils.isNotBlank(pageParam.getCode())) {
            queryWrapper.eq("b.code", pageParam.getCode());
        }
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            queryWrapper.like(SysBasDictDetail::getValue, pageParam.getSeachValue());
        }
        if (StringUtils.isNotBlank(pageParam.getKeywords())) {
            queryWrapper.like(SysBasDictDetail::getValue, pageParam.getKeywords());
        }

        queryWrapper.eq(SysBasDictDetail::getDelFlag, 0)
                .orderByAsc(SysBasDictDetail::getSx);

        List<Map<String, Object>> list = new ArrayList<>();
        List<SysBasDictDetail> dictList = sysBasDictDetailMapper.selectList(queryWrapper);

        Map<String, List<SysBasDictDetail>> collect = dictList.stream().collect(Collectors.groupingBy(SysBasDictDetail::getCommonId));
        Set<String> commonIds = collect.keySet();
        for (String commonId : commonIds) {
            List<SysBasDictDetail> dicts = collect.get(commonId);
            Map<String, Object> map = new HashMap<>();
            map.put("commonId", commonId);
            for (SysBasDictDetail dictitem : dicts) {
                map.put(dictitem.getCode(), dictitem.getValue());
                map.put("sx", dictitem.getSx());
                map.put("isParentEdit", dictitem.getIsParentEdit());
                map.put("grade", dictitem.getGrade());
                map.put("orgId",dictitem.getOrgId());
            }
            list.add(map);
        }
        List<Map<String, Object>> no = list.stream().sorted(comparing(l -> (Integer) l.get("sx"))).collect(Collectors.toList());
        return Result.succ(1, "查询成功", no);
    }

    //危急值提醒程序获取处理措施
    @Override
    public String findDictDetails(String code) {
        MPJLambdaWrapper<SysBasDictDetail> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(SysBasDictDetail.class)
                .selectAs("b", SysBasDict::getIsEdit, SysBasDictDetail::getIsParentEdit)
                .leftJoin(SysBasDict.class, "a", SysBasDict::getId, SysBasDictDetail::getPid)
                .leftJoin(SysBasDict.class, "b", SysBasDict::getId, SysBasDict::getPid);
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.eq("b.code", code);
        }

        queryWrapper.eq(SysBasDictDetail::getDelFlag, 0)
                .orderByAsc(SysBasDictDetail::getSx);

        String values = "";
        List<SysBasDictDetail> dictList = sysBasDictDetailMapper.selectList(queryWrapper);

        Map<String, List<SysBasDictDetail>> collect = dictList.stream().collect(Collectors.groupingBy(SysBasDictDetail::getCommonId));
        Set<String> commonIds = collect.keySet();
        for (String commonId : commonIds) {
            List<SysBasDictDetail> dicts = collect.get(commonId);
            Map<String, Object> map = new HashMap<>();
            map.put("commonId", commonId);
            for (SysBasDictDetail dictitem : dicts) {
                if (Objects.equals(dictitem.getCode(), "name")) {
                    values = values + dictitem.getValue() + "|";
                }
            }
        }
        values = values.substring(0, values.length() - 1);
        return values;
    }


    @Override
    public Result findDictDetailListByDictCodes(CommonPageDto pageParam) {

        //先根据codes查出所有的字典
        LambdaQueryWrapper<SysBasDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysBasDict::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getPid())) {
            String[] ids = pageParam.getPid().split(",");
            lambdaQueryWrapper.in(SysBasDict::getPid, ids);
        } else if (StringUtils.isNotBlank(pageParam.getCode())) {
            String[] codes = pageParam.getCode().split(",");
            lambdaQueryWrapper.in(SysBasDict::getCode, codes);
        }
        List<SysBasDict> dictList = sysBasDictMapper.selectList(lambdaQueryWrapper);

        //先根据codes查出所有的字典值
        MPJLambdaWrapper<SysBasDictDetail> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(SysBasDictDetail.class)
                .selectAs("b", SysBasDict::getIsEdit, SysBasDictDetail::getIsParentEdit)
                .selectAs("b", SysBasDict::getId, SysBasDictDetail::getPpid)
                .leftJoin(SysBasDict.class, "a", SysBasDict::getId, SysBasDictDetail::getPid)
                .leftJoin(SysBasDict.class, "b", SysBasDict::getId, SysBasDict::getPid);
        if (StringUtils.isNotBlank(pageParam.getPid())) {
            String[] ids = pageParam.getPid().split(",");
            queryWrapper.in(SysBasDict::getPid, ids);
        } else if (StringUtils.isNotBlank(pageParam.getCode())) {
            String[] codes = pageParam.getCode().split(",");
            queryWrapper.in("b.code", codes);
        }

        queryWrapper.eq(SysBasDictDetail::getDelFlag, 0)
                .orderByAsc(SysBasDictDetail::getSx);
        queryWrapper.eq(StrUtil.isNotEmpty(pageParam.getOrgId()),SysBasDictDetail::getOrgId,pageParam.getOrgId());

        List<SysBasDictDetail> dictDetailList = sysBasDictDetailMapper.selectList(queryWrapper);

        for (SysBasDict sysBasDict : dictList) {
            List<SysBasDictDetail> detailList = dictDetailList.stream().filter(de -> de.getPpid().equals(sysBasDict.getId())).collect(Collectors.toList());
            List<Map<String, Object>> list = new ArrayList<>();

            Map<String, List<SysBasDictDetail>> collect = detailList.stream().collect(Collectors.groupingBy(SysBasDictDetail::getCommonId));
            Set<String> commonIds = collect.keySet();
            for (String commonId : commonIds) {
                List<SysBasDictDetail> dicts = collect.get(commonId);
                Map<String, Object> map = new HashMap<>();
                map.put("commonId", commonId);
                for (SysBasDictDetail dictitem : dicts) {
                    map.put(dictitem.getCode(), dictitem.getValue());
                    map.put("sx", dictitem.getSx());
                    map.put("isParentEdit", dictitem.getIsParentEdit());
                    map.put("grade", dictitem.getGrade());
                }
                list.add(map);
            }
            List<Map<String, Object>> no = list.stream().sorted(comparing(l -> (Integer) l.get("sx"))).collect(Collectors.toList());
            sysBasDict.setDetailDataList(no);
        }


        return Result.succ(1, "查询成功", dictList);
    }

    @Override
    public Result getSysBasDictList(String code) {
        MPJLambdaWrapper<SysBasDictDetail> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(SysBasDictDetail.class)
                .selectAs("b", SysBasDict::getIsEdit, SysBasDictDetail::getIsParentEdit)
                .leftJoin(SysBasDict.class, "a", SysBasDict::getId, SysBasDictDetail::getPid)
                .leftJoin(SysBasDict.class, "b", SysBasDict::getId, SysBasDict::getPid)
                .eq("b.code", code);


        queryWrapper.eq(SysBasDictDetail::getDelFlag, 0)
                .orderByAsc(SysBasDictDetail::getSx);

        List<Map<String, Object>> list = new ArrayList<>();
        List<SysBasDictDetail> dictList = sysBasDictDetailMapper.selectList(queryWrapper);

        Map<String, List<SysBasDictDetail>> collect = dictList.stream().collect(Collectors.groupingBy(SysBasDictDetail::getCommonId));
        Set<String> commonIds = collect.keySet();
        for (String commonId : commonIds) {
            List<SysBasDictDetail> dicts = collect.get(commonId);
            Map<String, Object> map = new HashMap<>();
            map.put("commonId", commonId);
            for (SysBasDictDetail dictitem : dicts) {
                map.put(dictitem.getCode(), dictitem.getValue());
                map.put("sx", dictitem.getSx());
                map.put("isParentEdit", dictitem.getIsParentEdit());
                map.put("grade", dictitem.getGrade());
            }
            list.add(map);
        }
        List<Map<String, Object>> no = list.stream().sorted(comparing(l -> (Integer) l.get("sx"))).collect(Collectors.toList());
        return Result.succ(1, "查询成功", no);
    }

    @Override
    public Result getSysBasDictListByOrgId(String code, String orgId) {
        MPJLambdaWrapper<SysBasDictDetail> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(SysBasDictDetail.class)
                .selectAs("b", SysBasDict::getIsEdit, SysBasDictDetail::getIsParentEdit)
                .leftJoin(SysBasDict.class, "a", SysBasDict::getId, SysBasDictDetail::getPid)
                .leftJoin(SysBasDict.class, "b", SysBasDict::getId, SysBasDict::getPid)
                .eq("b.code", code);


        queryWrapper.eq(SysBasDictDetail::getDelFlag, 0)
                .eq(SysBasDictDetail::getOrgId, orgId)
                .orderByAsc(SysBasDictDetail::getSx);

        List<Map<String, Object>> list = new ArrayList<>();
        List<SysBasDictDetail> dictList = sysBasDictDetailMapper.selectList(queryWrapper);

        Map<String, List<SysBasDictDetail>> collect = dictList.stream().collect(Collectors.groupingBy(SysBasDictDetail::getCommonId));
        Set<String> commonIds = collect.keySet();
        for (String commonId : commonIds) {
            List<SysBasDictDetail> dicts = collect.get(commonId);
            Map<String, Object> map = new HashMap<>();
            for (SysBasDictDetail dictitem : dicts) {
                if ("code".equals(dictitem.getCode())||"name".equals(dictitem.getCode())){
                    map.put(dictitem.getCode(), dictitem.getValue());
                }
            }
            list.add(map);
        }
        return Result.succ(1, "查询成功", list);
    }

    @Override
    public Result saveOrUpdateDetail(SysBasDict dict) {
        Boolean falg = true;
        List<SysBasDictDetail> allDetailList = dict.getDetailList();
        List<SysBasDictDetail> addList = allDetailList.stream().filter(d -> StringUtils.isBlank(d.getCommonId())).collect(Collectors.toList());
        List<SysBasDictDetail> editList = allDetailList.stream().filter(d -> StringUtils.isNotBlank(d.getCommonId())).collect(Collectors.toList());
        Map<Integer, List<SysBasDictDetail>> detailGoups = addList.stream().collect(Collectors.groupingBy(SysBasDictDetail::getSx));
        for (Map.Entry<Integer, List<SysBasDictDetail>> entry : detailGoups.entrySet()) {
            List<SysBasDictDetail> newDetailList = entry.getValue();
            //String commonId = UUID.randomUUID().toString();
            String commonId = IdUtil.getSnowflake().nextId() + "";
            for (SysBasDictDetail detail : newDetailList) {
                detail.setCommonId(commonId);
            }
            //查看是否有标识字段，如果有标识字段则按照规则生成值
            List<SysBasDictDetail> collect = newDetailList.stream().filter(a -> StringUtils.isNotBlank(a.getClassify()) && a.getClassify().equals("1")).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)) {
                for (SysBasDictDetail detail : collect) {
                    //取前缀
                    String prefixType = detail.getPrefixType();
                    String prefix = detail.getPrefix();
                    String prefixText = CodeRoleUtils.getFixRoleText(prefixType, prefix, null);
                    //取中缀
                    String infixType = detail.getInfixType();
                    String infix = detail.getInfix();
                    String infixText = CodeRoleUtils.getFixRoleText(infixType, infix, null);
                    //取后缀
                    String suffixType = detail.getSuffixType();
                    String suffix = detail.getSuffix();
                    Integer suffLeth = 4;
                    if ("seq".equals(suffixType)) {
                        suffLeth = Integer.valueOf(suffix);
                    }

                    Integer integer = (int) (Math.pow(10, suffLeth)) + 1;
                    QueryWrapper<SysBasDictDetail> queryWrapperApp = new QueryWrapper<>();
                    //queryWrapperApp.select("max(CONVERT(INT,RIGHT(VALUE, "+suffLeth+"))) as VALUE")
                    queryWrapperApp.select("max(SUBSTR(VALUE, " + suffLeth * -1 + ", " + suffLeth + ")) as VALUE")
                            .like("VALUE", prefixText + infixText)
                            .eq("PID", detail.getPid());
                    SysBasDictDetail or = sysBasDictDetailMapper.selectOne(queryWrapperApp);
                    if (null == or) {
                        detail.setValue(prefixText + infixText + String.valueOf(integer).substring(1));
                    } else {
                        detail.setValue(prefixText + infixText + String.valueOf(Integer.valueOf(or.getValue()) + integer).substring(1));
                    }
                }
            }
            boolean b = this.saveBatch(newDetailList);
            if (!b) {
                falg = b;
            }
        }

        for (SysBasDictDetail detail : editList) {
            LambdaUpdateWrapper<SysBasDictDetail> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SysBasDictDetail::getPid, detail.getPid())
                    .eq(SysBasDictDetail::getCommonId, detail.getCommonId())
                    .set(SysBasDictDetail::getValue, detail.getValue())
                    .set(SysBasDictDetail::getSx, detail.getSx());
            boolean update = this.update(updateWrapper);
            if (!update) {
                boolean bb = this.save(detail);
                if (!bb) {
                    falg = bb;
                }

            }
        }
        if (falg) {
            return Result.succ(1, "更新成功", null);
        } else {
            return Result.fail("更新失败！");
        }
    }

    @Override
    public String getValueByCodeAndKay(String code, String kay) {
        Result result = this.getSysBasDictList(code);
        if (ToolsUtils.isEmpty(result.getData())) {
            return null;
        }
        List<Map<String, Object>> listMap = (List<Map<String, Object>>) result.getData();
        String value = "";
        for (Map<String, Object> map : listMap) {
            if (((String) map.get("code")).equals(kay)) {
                value = (String) map.get("name");
                break;
            }
        }
        return value;
    }


    @Override
    public Result addBatchDetail(SysBasDict dict) {
        Boolean falg = true;
        List<SysBasDictDetail> detailList = dict.getDetailList();
        Map<Integer, List<SysBasDictDetail>> detailGoups = detailList.stream().collect(Collectors.groupingBy(SysBasDictDetail::getSx));
        for (Map.Entry<Integer, List<SysBasDictDetail>> entry : detailGoups.entrySet()) {
            List<SysBasDictDetail> newDetailList = entry.getValue();
            //String commonId = UUID.randomUUID().toString();
            String commonId = IdUtil.getSnowflake().nextId() + "";
            for (SysBasDictDetail detail : newDetailList) {
                detail.setCommonId(commonId);
            }
            //查看是否有标识字段，如果有标识字段则按照规则生成值
            List<SysBasDictDetail> collect = newDetailList.stream().filter(a -> StringUtils.isNotBlank(a.getClassify()) && a.getClassify().equals("1")).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)) {
                for (SysBasDictDetail detail : collect) {
                    //取前缀
                    String prefixType = detail.getPrefixType();
                    String prefix = detail.getPrefix();
                    String prefixText = CodeRoleUtils.getFixRoleText(prefixType, prefix, null);
                    //取中缀
                    String infixType = detail.getInfixType();
                    String infix = detail.getInfix();
                    String infixText = CodeRoleUtils.getFixRoleText(infixType, infix, null);
                    //取后缀
                    String suffixType = detail.getSuffixType();
                    String suffix = detail.getSuffix();
                    Integer suffLeth = 4;
                    if (suffixType.equals("seq")) {
                        suffLeth = Integer.valueOf(suffix);
                    }

                    Integer integer = (int) (Math.pow(10, suffLeth)) + 1;
                    QueryWrapper<SysBasDictDetail> queryWrapperApp = new QueryWrapper<>();
                    //queryWrapperApp.select("max(CONVERT(INT,RIGHT(VALUE, "+suffLeth+"))) as VALUE")
                    queryWrapperApp.select("max(SUBSTR(VALUE, " + suffLeth * -1 + ", " + suffLeth + ")) as VALUE")
                            .like("VALUE", prefixText + infixText)
                            .eq("PID", detail.getPid());
                    SysBasDictDetail or = sysBasDictDetailMapper.selectOne(queryWrapperApp);
                    if (null == or) {
                        detail.setValue(prefixText + infixText + String.valueOf(integer).substring(1));
                    } else {
                        detail.setValue(prefixText + infixText + String.valueOf(Integer.valueOf(or.getValue()) + integer).substring(1));
                    }

                }
            }

            boolean b = this.saveBatch(newDetailList);
            if (!b) {
                falg = b;
            }
        }
        if (falg) {
            return Result.succ(1, "更新成功", null);
        } else {
            return Result.fail("更新失败！");
        }
    }

    @Override
    public Result editBatchDetail(SysBasDict dict) {
        List<SysBasDictDetail> detailList = dict.getDetailList();
        boolean b = true;
        for (SysBasDictDetail detail : detailList) {
            LambdaUpdateWrapper<SysBasDictDetail> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SysBasDictDetail::getPid, detail.getPid())
                    .eq(SysBasDictDetail::getCommonId, detail.getCommonId())
                    .set(SysBasDictDetail::getValue, detail.getValue())
                    .set(SysBasDictDetail::getSx, detail.getSx());
            boolean update = this.update(updateWrapper);
            if (!update) {
                boolean bb = this.save(detail);
                if (!bb) {
                    b = bb;
                }

            }
        }
        if (b) {
            return Result.succ(1, "更新成功", null);
        } else {
            return Result.fail("更新失败！");
        }
    }

    @Override
    public Result deleteBatchDetail(SysBasDictDetail detail) {
        LambdaQueryWrapper<SysBasDictDetail> delLam2 = new LambdaQueryWrapper<>();
        delLam2.eq(SysBasDictDetail::getCommonId, detail.getCommonId());
        int delete = sysBasDictDetailMapper.delete(delLam2);
        if (delete > 0) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败！");
        }
    }
    @Override
    public Result uploadDetailByExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String EXCEL_XLS = "xls";
        String EXCEL_XLSX = "xlsx";
        String dictId = request.getParameter("dictId");
        String grade = request.getParameter("grade");
        boolean b = false;

        //根据dictId和grade查出字典
        LambdaQueryWrapper<SysBasDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysBasDict::getDelFlag, 0);
        if (null != grade) {
            lambdaQueryWrapper.eq(SysBasDict::getGrade, grade);
        }
        if (StringUtils.isNotBlank(dictId)) {
            lambdaQueryWrapper.eq(SysBasDict::getPid, dictId);
        }
        lambdaQueryWrapper.orderByAsc(SysBasDict::getSx);
        List<SysBasDict> dictList = sysBasDictMapper.selectList(lambdaQueryWrapper);

        try {
            //根据路径获取这个操作excel的实例
            Sheet sheet = null;
            Row row = null;
            if (file.getOriginalFilename().endsWith(EXCEL_XLS)) {
                //用于xls文件的读取
                HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
                sheet = wb.getSheetAt(0);
            }
            else
            if (file.getOriginalFilename().endsWith(EXCEL_XLSX)) {
                //用于xlsx文件的读取
                XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
                //根据页面index 获取sheet页
                sheet = wb.getSheetAt(0);
            }

            List<SysBasDict> dictListLS = new ArrayList<>();
            //获取第一行的数据 取到相应的code
            row = sheet.getRow(0);
            if (Objects.nonNull(row)){
                int cellCount = row.getLastCellNum();
                for (int j = 0; j <= cellCount; j++) {
                    if (row.getCell(j) != null) {
                        Cell cell = row.getCell(j);
                        System.out.println(cell.getStringCellValue());
                        Optional<SysBasDict> first = dictList.stream().filter(d -> d.getCode().equals(cell.getStringCellValue())).findFirst();
                        if(first.isPresent()){
                            dictListLS.add(first.get());
                        }

                    }
                }
            }


            if(ToolsUtils.isNotEmpty(dictListLS)){
                List<SysBasDictDetail> allDetailList = new ArrayList<>();
                //每一行循环
                for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) {

                    //获取每一行数据
                    row = sheet.getRow(i);
                    if (Objects.nonNull(row)){
                        String commonId = IdUtil.getSnowflake().nextId() + "";
                        int size = dictListLS.size();

                        int cellCount = row.getLastCellNum();
                        for (int j = 0; j <= cellCount; j++) {
                            if (row.getCell(j) != null) {
                                Cell cell = row.getCell(j);
                                String cellValue = getCellValueAsString(cell);
                                if(j < size){
                                    SysBasDict sysBasDict = dictListLS.get(j);
                                    if (ToolsUtils.isNotEmpty(sysBasDict)){
                                        SysBasDictDetail addDetail = new SysBasDictDetail();
                                        addDetail.setPid(sysBasDict.getId());
                                        addDetail.setOrgId(sysBasDict.getOrgId());
                                        addDetail.setName(sysBasDict.getName());
                                        addDetail.setCode(sysBasDict.getCode());
                                        addDetail.setGrade(2);
                                        addDetail.setSx(i);
                                        addDetail.setValue(cellValue);
                                        addDetail.setCommonId(commonId);
                                        allDetailList.add(addDetail);
                                    }

                                }
                            }
                        }
                    }else {
                        continue;
                    }
                }
                if(ToolsUtils.isNotEmpty(allDetailList)){
                    b = this.saveBatch(allDetailList);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("excel读取错误");
        }

        if (b) {
            return Result.succ(1, "更新成功", null);
        } else {
            return Result.fail("更新失败！");
        }
    }
    /**
     * 根据单元格类型获取字符串值
     *
     * @param cell 单元格
     * @return 单元格的字符串值
     */
    private String getCellValueAsString(Cell cell) {
        if (cell == null) {
            return "";
        }

        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    double numericCellValue = cell.getNumericCellValue();
                    if(numericCellValue == Math.floor(numericCellValue)){
                        int numericCellValue1 = (int) numericCellValue;
                        return String.valueOf(numericCellValue1);
                    }else {
                        return String.valueOf(cell.getNumericCellValue());
                    }
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula(); // 或者计算公式结果
            default:
                return "";
        }
    }


}
